import os


def compare_files(file1_path, file2_path):
    # 打开两个文件
    with open(file1_path, 'r', encoding='utf-8') as file1, open(file2_path, 'r', encoding='utf-8') as file2:
        # 逐行读取文件内容
        lines1 = file1.readlines()
        lines2 = file2.readlines()

    # 获取两个文件中较长的行数
    max_lines = max(len(lines1), len(lines2))

    # 逐行对比
    for i in range(max_lines):
        # 获取当前行的内容，如果文件较短，则用空字符串代替
        line1 = lines1[i].strip() if i < len(lines1) else ""
        line2 = lines2[i].strip() if i < len(lines2) else ""

        # 如果两行内容不一样
        if line1 != line2:
            print(f"行号 {i + 1}:")
            print(f"文件1: {line1}")
            print(f"文件2: {line2}")
            print("-" * 40)  # 打印分隔线


# 调用函数
BASE_DIR_1 = "D:\workspace\\temp\\temp1.docx"
BASE_DIR_2 = "D:\workspace\\temp\\temp2.docx"
FILE_RELATIVE_PATH = "\word\document.xml"
file1_path = f"{BASE_DIR_1}{FILE_RELATIVE_PATH}"  # 替换为你的第一个文件路径
file2_path = f"{BASE_DIR_2}{FILE_RELATIVE_PATH}"  # 替换为你的第二个文件路径
compare_files(file1_path, file2_path)
